"""
订单管理序列化器
"""
from rest_framework import serializers
from orders.models import OrderInfo, OrderGoods
from goods.models import SKU

class OrderSimpleSerializer(serializers.ModelSerializer):
    """
    订单简单序列化器用于 列表数据返回
    """
    class Meta:
        model = OrderInfo
        fields = ['order_id', 'create_time', 'status']

        extra_kwargs = {
        'status': {'write_only': True},
        'create_time': {'format': '%Y.%m.%d'}
        }



class SKUSimpleSerializer(serializers.ModelSerializer):
    """
    自定义SKU序列化器
    """
    class Meta:
        model = SKU
        fields = ['name', 'default_image']

class OrderGoodsSerializer(serializers.ModelSerializer):
    """
    定义OrderGoods序列化器
    """
    sku = SKUSimpleSerializer()
    class Meta:
        model = OrderGoods
        fields = ['count', 'price', 'sku']

class OrderSerializer(serializers.ModelSerializer):
    """
    订单详情序列化器用于 单一订单详情序列化返回
    """
    user = serializers.StringRelatedField()
    skus = OrderGoodsSerializer(many = True)
    class Meta:
        model = OrderInfo
        fields = '__all__'

        extra_kwargs = {
        'create_time': {'format': '%Y.%m.%d'}
        }
        